<template>
	<view>
		<view class="top-tip">
			<view class="tip-title">紧急联系人</view>
			<view class="tip-text">最多可添加5位紧急联系人，紧急联系人在你使用一键报警时，可以收到你的求助和位置等相关信息</view>
		</view>
		<view class="list-view">
			<uni-swipe-action>
				<!-- 使用插槽 （请自行给定插槽内容宽度）-->
				<uni-swipe-action-item v-for="(item,index) in list">
					<view>
						<view class="list-item">
							<uni-icons type="person-filled" size="30"></uni-icons>
							<view class="phone">{{item.contactsTel}}</view>
							<view class="name">{{item.contactsName}}</view>
							<view class="expand"></view>
							<view class="right-text" @click="navToEdit(item)">编辑</view>
							<uni-icons type="right" size="20"></uni-icons>
						</view>
					</view>
					<template v-slot:right>
						<view class="del-view">
							<view class="del-btn" @click="delItem(item)">删除</view>
						</view>
					</template>
				</uni-swipe-action-item>
			</uni-swipe-action>

			<view class="list-add" @click="navToAdd" v-if="showAddBtn">
				<uni-icons type="plus-filled" size="30"></uni-icons>
				<view class="add-text">添加紧急联系人</view>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	import * as wxUserContacts from '../../common/api/wxUserContacts.js'
	export default {
		data() {
			return {
				list:[]
			}
		},
		onShow() {
			this.getList()
		},
		computed:{
			showAddBtn(){
				if(this.list && this.list.length >= 5){
					return false
				}
				return true
			}
		},
		methods: {
			getList(){
				const dataJson = uni.getStorageSync('user_info') || '{}'
				console.log('user_info dataJson',dataJson);
				const data = JSON.parse(dataJson) || {}
				wxUserContacts.queryByPage({
					phoneNum:data.tel,
				}).then(result => {
					this.list = result.data.data.rows || []
				}).catch(error => {
					console.log(error);
				})
			},
			delItem(item){
				uni.showModal({
					title:'提示',
					content:'确认删除？',
					success:(res)=> {
						if(res.confirm){
							const param = {
								uid:item.uid
							}
							wxUserContacts.deleteById(param).then(result => {
								uni.showToast({
									title:result.data.msg,
								})
								this.getList()
							}).catch(error => {
								console.log(error);
							})
						}

					}
				})
			},
			navToAdd(){
				uni.navigateTo({
					url:"/pages/addAndEditEmergencyContact/addAndEditEmergencyContact"
				})
			},
			navToEdit(item){
				uni.navigateTo({
					url:"/pages/addAndEditEmergencyContact/addAndEditEmergencyContact?uid="+item.uid
				})
			},
		}
	}
</script>
<style>
	page{
		background: white;
	}
</style>
<style lang="scss" scoped>
.top-tip{
	padding: 30rpx;
	.tip-title{
		font-size: 50rpx;
		font-weight: bold;
	}
	.tip-text{
		margin-top: 10rpx;
	}
}

.list-view{
	padding: 30rpx;
	box-sizing: border-box;
	.list-item{
		width: 100%;
		min-height: 100rpx;
		box-sizing:border-box;
		padding: 10rpx;
		background: #F5F6F8;
		display: flex;
		align-items: center;
		border-radius: 10rpx;
		margin-top: 20rpx;
		.phone{
			margin-left: 10rpx;
		}
		.name{
			margin-left: 20rpx;
		}
		.right-text{
			
		}
		.expand{
			flex: 1;
		}
	}
	.del-view{
		width: 120rpx;
		height: 100%;
		display: flex;
		align-items: center;
		padding: 10rpx;
		.del-btn{
			width: 100%;
			height: 100rpx;
			background: red;
			display: flex;
			align-items: center;
			justify-content: center;
			color: white;
			border-radius: 10rpx;
		}
	}
	.list-add{
		width: 100%;
		min-height: 100rpx;
		box-sizing:border-box;
		padding: 10rpx;
		background: #F5F6F8;
		display: flex;
		align-items: center;
		border-radius: 10rpx;
		margin-top: 20rpx;
		.add-text{
			margin-left: 10rpx;
		}
	}
}

</style>
